home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Files / ParseFullPathname / ParseFullPathName.c next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  3.5 KB  |  110 lines  |  [TEXT/MPS ]

  1. /*
  2. **
  3. **        ParseFullPathname
  4. **
  5. **            Takes a *complete* full pathname and turns it into a DirID and VRefNum pair.
  6. **            This does not handle partial pathnames because it assumes that the first element
  7. **            in the pathname is volume name.
  8. **
  9. **            to recurse or not to recurse, that is the question... (not)
  10. **
  11. **
  12. **            Neil Day
  13. **            MacDTS
  14. **            Apple Computer, Inc.
  15. **
  16. */
  17.  
  18. #include <stdio.h>                                                            // Compiler Interfaces
  19. #include <files.h>
  20. #include <String.h>
  21. #include <Strings.h>
  22. #include <Memory.h>
  23. #include <Errors.h>
  24.  
  25. OSErr ParseFullPathname (FSSpec *Result, char *PathNamePtr);                // function prototypes
  26. Boolean GetElement (StringPtr Result,char * PathNamePtr,short ElementNumber);
  27.  
  28. main (int argc,char ** argv)
  29. {
  30.     OSErr        err;
  31.     FSSpec        here;
  32.     
  33.     if (argc != 2)
  34.         printf ("form: pfpn <pathname>\n");
  35.  
  36.     err = ParseFullPathname (&here, argv[1]);
  37.     if (err)
  38.         printf ("Died of error %d\n",err);
  39.     else
  40.         printf ("vRefNum %d DirID %d name %P\n",here.vRefNum, here.parID,here.name);
  41. }
  42.  
  43. Boolean GetElement (StringPtr Result,char *PathNamePtr,short ElementNumber)
  44. {
  45.     char             *eStart, *eEnd;
  46.     
  47.     eStart = eEnd = PathNamePtr;
  48.     
  49.     while (ElementNumber) {                                                    // Search for the element
  50.         if (*eEnd == ':' || !(*eEnd)) {                                        //   if we see colon or a null , we're at the end of element
  51.             --ElementNumber;                                                //   one down, n-1 to go
  52.             if (ElementNumber == 1)                                            //   are we at the second to last element??
  53.                 eStart = eEnd + 1;                                            //        mark it.
  54.         }
  55.         if (!(*eEnd)) break;
  56.         ++eEnd;                                                                //   always increment 
  57.     }
  58.     
  59.     if (ElementNumber || (eEnd - eStart > 32) || (eEnd - eStart == 0))        // If n > 0 or the element is too big or there is no element
  60.         return false;                                                         //  then croak.
  61.  
  62.     BlockMove ((Ptr) eStart, (Ptr) (Result + 1),                            // Move the substring into the Result
  63.         *Result = (char)eEnd - eStart);
  64.     
  65.     return true;
  66. }
  67.  
  68.  
  69. OSErr ParseFullPathname (FSSpec *Result,char *PathNamePtr)
  70. {
  71.     OSErr            err;
  72.     short            level = 2;
  73.     CInfoPBRec        dInfo;
  74.     HParamBlockRec    vInfo;
  75.  
  76.     Result->parID = fsRtDirID;                                                // start at the top of the volume
  77.  
  78.     if (GetElement ((StringPtr) &Result->name, PathNamePtr, 1)) {            // If there is a volume name
  79.         --(*Result->name);
  80.         vInfo.volumeParam.ioVRefNum =     0;                                    //   Get its vRefNum...
  81.         vInfo.volumeParam.ioNamePtr =    &Result->name;
  82.         vInfo.volumeParam.ioVolIndex =    -1;
  83.         err = PBHGetVInfo (&vInfo,false);
  84.         printf ("err %d\n",err);
  85.         if (err) return err;                                                //   and return an error if we couldn't find it
  86.         Result->vRefNum = vInfo.volumeParam.ioVRefNum;                        //   ...or put it into the Result
  87.     }
  88.  
  89.     while (GetElement ((StringPtr) &Result->name, PathNamePtr, level)) {    // Walk down the pathname
  90.         printf ("%P, %d, %c\n",Result->name, *Result->name, (Result->name + (*Result->name) -1 ));
  91.         if ((Result->name + (*Result->name-1)) == ':' ) {                        //   if the last char is a colon
  92.             --*(Result->name);                                                //   get rid of the colon on the end of the element
  93.             printf ("%P\n",Result->name);
  94.         }
  95.         dInfo.dirInfo.ioFDirIndex = 0;                                        //   and get it's DirID
  96.         dInfo.dirInfo.ioDrDirID = Result->parID;
  97.         dInfo.dirInfo.ioVRefNum = Result->vRefNum;
  98.         dInfo.dirInfo.ioNamePtr = &Result->name;
  99.         err = PBGetCatInfo (&dInfo,false);
  100.         if (err) return err;                                                //   if there was an error, return it
  101.         Result->parID = dInfo.dirInfo.ioDrDirID;                            //   otherwise, update the current DirID
  102.         ++level;                                                            //   go on to the next level
  103.     }
  104.     
  105.     printf ("what's left over: %s\n",PathNamePtr);
  106.  
  107.     
  108.     return noErr;                                                            // no problems
  109. }
  110.